package dev.browse;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JEditorPane;

class WebBrowserPane extends JEditorPane{
	private static final long serialVersionUID = -4303216650208507282L;
	
	private WebBrowserPane browserPane;
	private List<URL> history = new ArrayList<URL>();

	private int historyIndex;

	public WebBrowserPane() {
		setEditable(false);
		browserPane = this;
		browserPane.setDropTarget(new DropTarget(browserPane,
					DnDConstants.ACTION_COPY, new DropTargetHandler()));
	}

	public void goToURL(URL url) {
		displayPage(url);
		history.add(url);
		historyIndex = history.size() - 1;
	}
	public String getTitle(){
		String text = this.getText();
		int start = text.indexOf("<title>") + 7;
		int end = text.indexOf("</title>");
		if(end>start && start>=0){
			return  text.substring(start, end);
		}else{
			return " ";
		}
	}
	public URL forward() {
		historyIndex++;
		if (historyIndex >= history.size()){
			historyIndex = history.size() - 1;
		}
		if (history==null || history.size()==0){
			return null;
		}
		URL url = (URL) history.get(historyIndex);
		displayPage(url);
		return url;
	}

	public URL back() {
		historyIndex--;
		if (historyIndex < 0){
			historyIndex = 0;
		}
		if (history==null || history.size()==0){
			return null;
		}
		URL url = (URL) history.get(historyIndex);
		displayPage(url);
		return url;
	}

	private void displayPage(URL pageURL) {
		try {
			setPage(pageURL);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}
	
	private class DropTargetHandler implements DropTargetListener {
		@SuppressWarnings("unchecked")
		public void drop(DropTargetDropEvent event) {
			Transferable transferable = event.getTransferable();
			if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
				event.acceptDrop(DnDConstants.ACTION_COPY);
				try {
					List<File> fileList = ((List<File>)transferable.getTransferData(DataFlavor.javaFileListFlavor));
					Iterator<File> iterator = fileList.iterator();
					while (iterator.hasNext()) {
						File file = (File) iterator.next();
						browserPane.goToURL(file.toURI().toURL());
					}
					event.dropComplete(true);
				} catch (UnsupportedFlavorException flavorException) {
					flavorException.printStackTrace();
					event.dropComplete(false);
				} catch (IOException ioException) {
					ioException.printStackTrace();
					event.dropComplete(false);
				}
			} else {
				event.rejectDrop();
			}
		}
		public void dragEnter(DropTargetDragEvent event) {
			if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
				event.acceptDrag(DnDConstants.ACTION_COPY);
			else {
				event.rejectDrag();
			}
		}
		public void dragExit(DropTargetEvent event) {}
		public void dragOver(DropTargetDragEvent event) {}
		public void dropActionChanged(DropTargetDragEvent event) {}

	}

}
